const jsdom = require("jsdom");

String.prototype.ht = function() {
  //   const parser = new DOMParser();
  //   const dom = parser.parseFromString(this, "text/html");

  const dom = new jsdom.JSDOM(this);
  return dom.window.document.querySelector("body").textContent;
};

fetch(
  "https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/2208747_indicators.txt",
  {
    headers: {
      accept: "application/json, text/javascript, */*; q=0.01",
      "sec-ch-ua":
        '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"',
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": '"Windows"',
      "x-requested-with": "XMLHttpRequest"
    },
    referrer: "https://www.qschina.cn/",
    referrerPolicy: "strict-origin",
    body: null,
    method: "GET",
    mode: "cors",
    credentials: "omit"
  }
)
  .then(res => res.json())
  .then(data => {
    let temp = "";
    data.data.forEach(e => {
      temp += `${e.overall_rank.padStart(3, "0")}: ${e.uni.ht()} 
      location: ${e.location} 
      region: ${e.region}\\n\\n`;
    });

    console.log(temp);
  });

// function getText() {
//   const parser = new DOMParser()
//   const htmlDoc = parser.parseFromString(htmlString, "text/html")

//   return htmlDoc.innerText
// }
